package com.io.压缩20M文件从30秒到1秒的优化过程;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Test1 {
	
	private static final String ZIP_FILE = "D:/zsl/2.zip";
	private static final String JPG_FILE = "D:/zsl/1.jpg";
	private static final String FILE_NAME = "1.jpg";
	
	public static void zipFileNoBuffer() {
	    File zipFile = new File(ZIP_FILE);
	    try (ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile))) {
	        //开始时间
	        long beginTime = System.currentTimeMillis();

            try (InputStream input = new FileInputStream(JPG_FILE)) {
                zipOut.putNextEntry(new ZipEntry(FILE_NAME));
                int temp = 0;
                while ((temp = input.read()) != -1) {
                    zipOut.write(temp);
                }
            }
	        long endTime = System.currentTimeMillis();
	        System.out.println(endTime - beginTime);
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	}
	
	public static void main(String[] args) {
		zipFileNoBuffer();
	}

}
